import request from './request'
import type { ApiResponse } from './types'

// 考核填写数据类型
export interface AssessmentFillData {
  id: number
  assessmentId: number
  assessmentName: string
  employeeId: number
  employeeName: string
  employeeNumber: string
  department: string
  fillStatus: 'not_started' | 'in_progress' | 'completed' | 'submitted' | 'approved' | 'rejected'
  submitTime?: string
  approveTime?: string
  createTime: string
  updateTime: string
}

// KPI填写详情数据类型
export interface KPIFillDetailData {
  id: number
  fillId: number
  kpiId: number
  kpiName: string
  kpiType: string
  kpiWeight: number
  selfScore?: number
  selfComment?: string
  managerScore?: number
  managerComment?: string
  finalScore?: number
  status: 'not_started' | 'self_filled' | 'manager_reviewed' | 'completed'
  createTime: string
  updateTime: string
}

// 考核填写分页查询参数
export interface AssessmentFillPageParams {
  current: number
  size: number
  assessmentId?: number
  employeeId?: number
  employeeName?: string
  fillStatus?: string
  department?: string
}

// 考核填写分页响应数据
export interface AssessmentFillPageResponse {
  records: AssessmentFillData[]
  total: number
  size: number
  current: number
}

// 获取考核填写列表
export const getAssessmentFillList = (assessmentId: number) => {
  return request<ApiResponse<AssessmentFillData[]>>({
    url: '/assessmentFills/list',
    method: 'post',
    data: { assessmentId }
  })
}

// 获取考核填写分页列表
export const getAssessmentFillPage = (params: AssessmentFillPageParams) => {
  return request<ApiResponse<AssessmentFillPageResponse>>({
    url: '/assessmentFills/page',
    method: 'post',
    data: params
  })
}

// 获取考核填写详情
export const getAssessmentFillDetail = (id: number) => {
  return request<ApiResponse<{
    fillInfo: AssessmentFillData
    kpiDetails: KPIFillDetailData[]
  }>>({
    url: '/assessmentFills/detail',
    method: 'post',
    data: { id }
  })
}

// 开始填写考核
export const startAssessmentFill = (assessmentId: number, employeeId: number) => {
  return request<ApiResponse<number>>({
    url: '/assessmentFills/start',
    method: 'post',
    data: { assessmentId, employeeId }
  })
}

// 提交KPI自评分数
export const submitKPISelfScore = (fillDetailId: number, score: number, comment: string) => {
  return request<ApiResponse<boolean>>({
    url: '/assessmentFills/submit-self-score',
    method: 'post',
    data: { fillDetailId, score, comment }
  })
}

// 提交经理评分
export const submitKPIManagerScore = (id: number, managerScore: number, managerComment: string) => {
  return request<ApiResponse<boolean>>({
    url: '/kpiFillDetails/updateManagerScore',
    method: 'post',
    data: { id, managerScore, managerComment }
  })
}

// 提交最终评分
export const submitKPIFinalScore = (id: number, finalScore: number) => {
  return request<ApiResponse<boolean>>({
    url: '/kpiFillDetails/updateFinalScore',
    method: 'post',
    data: { id, finalScore }
  })
}

// 提交整个考核填写
export const submitAssessmentFill = (fillId: number) => {
  return request<ApiResponse<boolean>>({
    url: '/assessmentFills/submit',
    method: 'post',
    data: { fillId }
  })
}

// 审批考核填写
export const approveAssessmentFill = (fillId: number, approved: boolean, comment?: string) => {
  return request<ApiResponse<boolean>>({
    url: '/assessmentFills/approve',
    method: 'post',
    data: { fillId, approved, comment }
  })
}

// ai分析
export const aiAnalysis = (fillId: number) => {
  return request<ApiResponse<boolean>>({
    url: '/kpiFillDetails/aiAnalysis',
    method: 'post',
    data: { fillId }
  })
}

// 获取员工待填写的考核列表
export const getEmployeePendingFills = (employeeId: number) => {
  return request<ApiResponse<AssessmentFillData[]>>({
    url: '/assessmentFills/employee-pending',
    method: 'post',
    data: { employeeId }
  })
}

// 获取考核填写统计数据
export const getAssessmentFillStats = (assessmentId: number) => {
  return request<ApiResponse<{
    total: number
    notStarted: number
    inProgress: number
    completed: number
    submitted: number
    approved: number
    rejected: number
    avgProgress: number
  }>>({
    url: '/assessmentFills/stats',
    method: 'post',
    data: { assessmentId }
  })
}

// countFillStatusByDepartment
export const countFillStatusByDepartment = (assessmentId: number) => {
  return request<ApiResponse<{
    assessmentId: number
  }[]>>({
    url: '/assessmentFills/countFillStatusByDepartment',
    method: 'post',
    data: { assessmentId }
  })
}
